<%#
 Copyright 2013-2019 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
package <%= packageName %>.service;

import io.github.jhipster.config.JHipsterProperties;
import <%= packageName %>.config.audit.AuditEventConverter;
<%_ if (reactive) { _%>
import <%= packageName %>.domain.PersistentAuditEvent;
<%_ } _%>
import <%= packageName %>.repository.PersistenceAuditEventRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.actuate.audit.AuditEvent;
<%_ if (!reactive) { _%>
import org.springframework.data.domain.Page;
<%_ } _%>
import org.springframework.data.domain.Pageable;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
<%_ if (databaseType === 'sql') { _%>
import org.springframework.transaction.annotation.Transactional;
<%_ } _%>
<%_ if (reactive) { _%>
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
<%_ } _%>

import java.time.Instant;
import java.time.temporal.ChronoUnit;
<%_ if (!reactive) { _%>
import java.util.Optional;
<%_ } _%>
<%_ if (reactive) { _%>
import java.util.HashMap;
import java.util.Map;

import static org.springframework.boot.actuate.security.AuthenticationAuditListener.AUTHENTICATION_FAILURE;
import static org.springframework.boot.actuate.security.AuthenticationAuditListener.AUTHENTICATION_SUCCESS;
<%_ } _%>

/**
 * Service for managing audit events.
 * <p>
 * This is the default implementation to support SpringBoot Actuator {@code AuditEventRepository}.
 */
@Service<% if (databaseType === 'sql') { %>
@Transactional<% } %>
public class AuditEventService {

    <%_ if (reactive) { _%>
    /**
     * Should be the same as in Liquibase migration.
     */
    private static final int EVENT_DATA_COLUMN_MAX_LENGTH = 255;

    <%_ } _%>
    private final Logger log = LoggerFactory.getLogger(AuditEventService.class);

    private final JHipsterProperties jHipsterProperties;

    private final PersistenceAuditEventRepository persistenceAuditEventRepository;

    private final AuditEventConverter auditEventConverter;

    public AuditEventService(
        PersistenceAuditEventRepository persistenceAuditEventRepository,
        AuditEventConverter auditEventConverter, JHipsterProperties jhipsterProperties) {

        this.persistenceAuditEventRepository = persistenceAuditEventRepository;
        this.auditEventConverter = auditEventConverter;
        this.jHipsterProperties = jhipsterProperties;
    }

    /**
    * Old audit events should be automatically deleted after 30 days.
    *
    * This is scheduled to get fired at 12:00 (am).
    */
    @Scheduled(cron = "0 0 12 * * ?")
    public void removeOldAuditEvents() {
        persistenceAuditEventRepository
            .findByAuditEventDateBefore(Instant.now().minus(jHipsterProperties.getAuditEvents().getRetentionPeriod(), ChronoUnit.DAYS))
            <%_ if (!reactive) { _%>
            .forEach(auditEvent -> {
                log.debug("Deleting audit data {}", auditEvent);
                persistenceAuditEventRepository.delete(auditEvent);
            });
            <%_ } else { _%>
            .flatMap(auditEvent -> {
                log.debug("Deleting audit data {}", auditEvent);
                return persistenceAuditEventRepository.delete(auditEvent);
            }).blockLast();
            <%_ } _%>
    }

    public <% if (reactive) { %>Flux<% } else { %>Page<% } %><AuditEvent> findAll(Pageable pageable) {
        return persistenceAuditEventRepository.findAll<% if (reactive) { %>By<% } %>(pageable)
            .map(auditEventConverter::convertToAuditEvent);
    }

    public <% if (reactive) { %>Flux<% } else { %>Page<% } %><AuditEvent> findByDates(Instant fromDate, Instant toDate, Pageable pageable) {
        return persistenceAuditEventRepository.findAllByAuditEventDateBetween(fromDate, toDate, pageable)
            .map(auditEventConverter::convertToAuditEvent);
    }

    public <% if (reactive) { %>Mono<% } else { %>Optional<% } %><AuditEvent> find(<% if (databaseType === 'sql') { %>Long <% } %><% if (databaseType === 'mongodb' || databaseType === 'couchbase') { %>String <% } %>id) {
        return persistenceAuditEventRepository.findById(id)
            .map(auditEventConverter::convertToAuditEvent);
    }
    <%_ if (reactive) { _%>

    public Mono<Long> count() {
        return persistenceAuditEventRepository.count();
    }

    public Mono<Long> countByDates(Instant fromDate, Instant toDate) {
        return persistenceAuditEventRepository.countByAuditEventDateBetween(fromDate, toDate);
    }

    public Mono<PersistentAuditEvent> saveAuthenticationSuccess(String login) {
        PersistentAuditEvent persistentAuditEvent = new PersistentAuditEvent();
        persistentAuditEvent.setPrincipal(login);
        persistentAuditEvent.setAuditEventType(AUTHENTICATION_SUCCESS);
        persistentAuditEvent.setAuditEventDate(Instant.now());
        return persistenceAuditEventRepository.save(persistentAuditEvent);
    }

    public Mono<PersistentAuditEvent> saveAuthenticationError(String login, Throwable e) {
        PersistentAuditEvent persistentAuditEvent = new PersistentAuditEvent();
        persistentAuditEvent.setPrincipal(login);
        persistentAuditEvent.setAuditEventType(AUTHENTICATION_FAILURE);
        persistentAuditEvent.setAuditEventDate(Instant.now());
        Map<String, String> eventData = new HashMap<>();
        eventData.put("type", e.getClass().getName());
        eventData.put("message", e.getMessage());
        persistentAuditEvent.setData(truncate(eventData));
        return persistenceAuditEventRepository.save(persistentAuditEvent);
    }

    /**
     * Truncate event data that might exceed column length.
     */
    private Map<String, String> truncate(Map<String, String> data) {
        Map<String, String> results = new HashMap<>();

        if (data != null) {
            for (Map.Entry<String, String> entry : data.entrySet()) {
                String value = entry.getValue();
                if (value != null) {
                    int length = value.length();
                    if (length > EVENT_DATA_COLUMN_MAX_LENGTH) {
                        value = value.substring(0, EVENT_DATA_COLUMN_MAX_LENGTH);
                        log.warn("Event data for {} too long ({}) has been truncated to {}. Consider increasing column width.",
                                entry.getKey(), length, EVENT_DATA_COLUMN_MAX_LENGTH);
                    }
                }
                results.put(entry.getKey(), value);
            }
        }
        return results;
    }
    <%_ } _%>
}
